package com.example.moreeyes;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity{
    Fragment home = null;
    Fragment discover = null;
    Fragment mine = null;

    private BottomNavigationView bottomNavigationView;
    private ViewPager2 viewPager2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewPager2 viewPager2 = findViewById(R.id.viewPager);

        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new DiscoverFragment());
        fragmentList.add(new MineFragment());

        ViewPager2Adapter viewPager2Adapter = new ViewPager2Adapter(this,fragmentList);
        viewPager2.setAdapter(viewPager2Adapter);

        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                if (position == 0){
                    bottomNavigationView.setSelectedItemId(R.id.nav_home);
                }else if (position == 1){
                    bottomNavigationView.setSelectedItemId(R.id.nav_discover);
                }else if (position == 2){
                    bottomNavigationView.setSelectedItemId(R.id.nav_mine);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });

        bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectFragment = null;
                switch (item.getItemId()){
                    case R.id.nav_home:
                        viewPager2.setCurrentItem(0);
                        break;
                    case R.id.nav_discover:
                        viewPager2.setCurrentItem(1);
                        break;
                    case R.id.nav_mine:
                        viewPager2.setCurrentItem(2);
                        break;
                }
                return true;
            }
        });
        bottomNavigationView.setSelectedItemId(R.id.nav_home);
    }
    private Fragment getHomeFragment(){
        if (home == null){
            home = new HomeFragment();
        }
        return home;
    }
    private Fragment getDiscoverFragment(){
        if (discover == null){
            discover = new DiscoverFragment();
        }
        return discover;
    }
    private Fragment getMineFragment(){
        if (mine == null){
            mine = new MineFragment();
        }
        return mine;
    }

    /**
     * 添加一个自定义的button
     */
    private void addOneButton() {
        Button button = new Button(this);
        button.setText("test");
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
             LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
                );
        button.setLayoutParams(params);
        ///获取跟Activity的顶层视图
        View decorView = getWindow().getDecorView();

//        FrameLayout layout = findViewById(R.id.fragment_container);
//        layout.addView(button);
    }
}